package tod141.medlemsliste.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import no.medlemsliste.dao.MedlemDAO;
import no.medlemsliste.dao.jdbc.MedlemDAOImpl;
import tod141.medlemsliste.model.Endringer;
import tod141.medlemsliste.utilities.RequestException;
import tod141.medlemsliste.utilities.ServletUtilities;

public class EndringerHent extends HttpServlet {
  private static final long serialVersionUID = 1L;

  // URL på Eple:
  //	http://eple.hib.no/t/bki/tod141/medlemsliste/EndringerHent?loggId=-1

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Endringer endringer=null;
    try {
      String loggIdS = request.getParameter("loggId");
      if (loggIdS == null) throw new RequestException ("Parameter loggId mangler.");
      int loggId=-1;
      loggId = Integer.parseInt(loggIdS);
      if (loggId < -1) throw new RequestException ("Parameter loggId har feil verdi."); 
      MedlemDAO medlemDAO = new MedlemDAOImpl();
      endringer = medlemDAO.hentEndringer(loggId);
      ServletUtilities.sendResponse(response,endringer);
    } catch(Exception e) {
      ServletUtilities.sendResponse(response,false);
    }
  }

  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String adresse="/WEB-INF/feilBruk.jsp";
    RequestDispatcher dispatcher = request.getRequestDispatcher(adresse);
    dispatcher.forward(request,response);
  }
}
